package com.stress.testing.common.conf;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

public class BaseConfig implements Config {
    private static final String SEPARATOR = ";";
    protected Properties pros;

    public BaseConfig(Properties pros) {
        this.pros = pros;
    }

    @Override
    public String get(String key, String defaultValue) {
        return pros.getProperty(key, defaultValue);
    }

    @Override
    public int getInt(String key, int defaultValue) {
        String property = pros.getProperty(key);

        if (property != null && property.length() > 0) {
            try {
                return Integer.parseInt(property);
            } catch (Exception e) {

            }
        }

        return defaultValue;
    }

    @Override
    public boolean getBoolean(String key, boolean defaultValue) {
        String property = pros.getProperty(key);

        if (property != null && property.length() > 0) {
            try {
                return Boolean.parseBoolean(property);
            } catch (Exception e) {

            }
        }

        return defaultValue;
    }

    @Override
    public long getLong(String key, long defaultValue) {
        String property = pros.getProperty(key);

        if (property != null && property.length() > 0) {
            try {
                return Long.parseLong(property);
            } catch (Exception e) {

            }
        }

        return defaultValue;
    }

    @Override
    public double getDouble(String key, double defaultValue) {
        String property = pros.getProperty(key);

        if (property != null && property.length() > 0) {
            try {
                return Double.parseDouble(property);
            } catch (Exception e) {

            }
        }

        return defaultValue;
    }

    @Override
    public List<String> getList(String key) {
        List<String> list = null;
        String property = pros.getProperty(key);

        if (property != null && property.length() > 0) {
            String[] split = property.split(SEPARATOR);
            list = Arrays.asList(split);
        } else {
            list = new ArrayList<>();
        }

        return list;
    }
}
